package ym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ym.mapper.HouseMapper;
import ym.pojo.House;
import ym.pojo.HouseBroker;
import ym.pojo.HouseImg;
import ym.pojo.HousePlot;
import ym.service.HouseBrokerService;
import ym.service.HouseImgService;
import ym.service.HousePlotService;
import ym.service.HouseService;

import java.util.List;

@Transactional
@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private HouseImgService houseImgService;
    @Autowired
    private HouseBrokerService houseBrokerService;
    @Autowired
    private HousePlotService housePlotService;


    @Override
    public void add(House house) {
        houseMapper.insert(house);
    }

    @Override
    public void update(House house) {
        houseMapper.updateById(house);
    }

    @Override
    public void updateStatus(Integer id) {
        House house = houseMapper.selectById(id);
        if (house.getHouseUse().equals("1")) {
            house.setHouseUse("0");
        } else {
            house.setHouseUse("1");
        }
        houseMapper.updateById(house);
    }

    @Override
    public void delete(Integer id) {
        houseMapper.deleteById(id);
        //删除房源对应的图片
        List<HouseImg> houseImgList = houseImgService.findAllByHouseId(id);
        for (HouseImg houseImg : houseImgList) {
            houseImgService.delete(houseImg.getHouseImgId());
        }
    }

    @Override
    public House findById(Integer id) {
        House house = houseMapper.selectById(id);
        //封装图片
        List<HouseImg> houseImgList = houseImgService.findAllByHouseId(id);
        house.setHouseImgList(houseImgList);
        //封装小区
        HousePlot housePlot = housePlotService.findById(house.getHousePlotId());
        house.setHousePlot(housePlot);
        //封装经纪人
        HouseBroker houseBroker = houseBrokerService.findById(house.getHouseBrokerId());
        house.setHouseBroker(houseBroker);
        return house;
    }

    @Override
    public List<House> findAll() {
        return houseMapper.selectList(null);
    }

    @Override
    public List<House> findAllByPlotId(Integer housePlotId) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("housePlotId", housePlotId).eq("houseUse", "1");
        return houseMapper.selectList(queryWrapper);
    }

    @Override
    public Page<House> search(String search, int page, int size) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("houseTitle", search);
        }
        return houseMapper.selectPage(new Page(page, size), queryWrapper);
    }

    @Override
    public Page<House> searchWithUse(String search, int page, int size) {
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        if (search != null) {
            queryWrapper.like("houseTitle", search).eq("houseUse", "1");
        }
        queryWrapper.eq("houseUse", "1");
        return houseMapper.selectPage(new Page(page, size), queryWrapper);
    }
}
